//
//  WJMainTabBar.swift
//  WJWeibo
//
//  Created by Jie on 15/12/16.
//  Copyright © 2015年 周文杰. All rights reserved.
//

import UIKit

class WJMainTabBar: UITabBar {


    //控件数量
    private let count = 5
    
    override func layoutSubviews() {
        //调用父类的该方法
        super.layoutSubviews()
        
        //计算每个控件的宽度
        let width = self.bounds.size.width / CGFloat(count)
        
        let frame = CGRect(x: 0, y: 0, width: width, height: bounds.height)
        
        //索引 遍历控件
        var index = 0
        
        //遍历子控件
        for view in subviews {
            
            if view is UIControl && !(view is UIButton){
                view.frame = CGRectOffset(frame, CGFloat(index) * width, 0)
                
                index += index == 1 ? 2 : 1
            }
            
        }
        addBtn.frame = CGRectOffset(frame, 2 * width, 0)
        
    }
    
    
    //添加中间按钮
    lazy var addBtn : UIButton = {
       
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
       
        self.addSubview(btn)
        return btn
        
    }()
    

}
